package fr.umlv.jee.jawp.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.umlv.jee.jawp.persistence.object.Category;
import fr.umlv.jee.jawp.persistence.object.Order;
import fr.umlv.jee.jawp.persistence.object.Product;
import fr.umlv.jee.jawp.service.aws.Aws;

@Service("amazonService")
public class AmazonService {

  @Transactional
  public List<Map<String, String>> findArticleByKeyword(String keyword) {
    Aws aws = new Aws();
    List<Map<String, String>> list = aws.findArticleByKeyword(keyword);
    System.out.println(list.size());
    return list;
  }

  @Transactional
  public Map<String, String> findArticleByItemID(String itemID) {
    Aws aws = new Aws();
    List<Map<String, String>> list = aws.findArticleByItemID(itemID);
    return list.get(0);
  }

  @Transactional
  public List<Map<String, String>> findArticleByKeywordOnBDD(ProductService p,
      String keyword) {
    Aws aws = new Aws();
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();

    String asin;
    for (Map<String, String> map : aws.findArticleByKeyword(keyword)) {
      asin = map.get("ASIN");
      if (p.getProductByASIN(asin) != null) {
        list.add(map);
      }
    }
    return list;
  }
  
  @Transactional
  public List<Map<String, String>> findArticleByCategory(ProductService prodService,
      Category cat) {
    Aws aws = new Aws();
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();

    for(Product prod : prodService.getProductsByCategory(cat.getId())) {
      Map<String, String> map = aws.findArticleByKeyword(prod.getASIN()).get(0);
      list.add(map);
    }
    return list;
  }

  @Transactional
  public List<Map<String, String>> findArticleByOrder(OrderService orderService,
      Order order) {
    Aws aws = new Aws();
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    List<Product> listProd = orderService.getProductByOrder(order);
    
    
    for(Product prod : listProd) {
      Map<String, String> map = aws.findArticleByKeyword(prod.getASIN()).get(0);
      list.add(map);
    }
    return list;
  }

}
